home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / sticpsrc.lzh / SOURCE.ARC / TCPOUT.C < prev    next >
C/C++ Source or Header  |  1988-08-24  |  6KB  |  189 lines

  1. #include "global.h"
  2. #include "timer.h"
  3. #include "mbuf.h"
  4. #include "netuser.h"
  5. #include "internet.h"
  6. #include "tcp.h"
  7.  
  8. /* Send a segment on the specified connection. One gets sent only
  9.  * if there is data to be sent or if "force" is non zero
  10.  */
  11. void
  12. tcp_output(tcb)
  13. register struct tcb *tcb;
  14. {
  15.     struct pseudo_header ph;/* Pseudo-header for checksum calcs */
  16.     struct mbuf *hbp,*dbp;    /* Header and data buffer pointers */
  17.     int16 hsize;        /* Size of header */
  18.     struct tcp seg;        /* Local working copy of header */
  19.     int16 ssize;        /* Size of current segment being sent,
  20.                  * including SYN and FIN flags */
  21.     int16 dsize;        /* Size of segment less SYN and FIN */
  22.     int16 usable;        /* Usable window */
  23.     int16 sent;        /* Sequence count (incl SYN/FIN) already in the pipe */
  24.  
  25.     if(tcb == NULLTCB)
  26.         return;
  27.  
  28.     switch(tcb->state){
  29.     case LISTEN:
  30.     case CLOSED:
  31.         return;    /* Don't send anything */
  32.     }
  33.     for(;;){
  34.         sent = tcb->snd.ptr - tcb->snd.una;
  35.  
  36. #ifdef    notdef
  37.         /* If this is a retransmission, send only the oldest segment
  38.          * (first-only retransmission policy) -- OBSOLETED by cwind
  39.          */
  40.         if((tcb->flags & RETRAN) && sent != 0)
  41.             break;
  42. #endif
  43.         /* Don't send anything else until our SYN has been acked */
  44.         if(sent != 0 && !(tcb->flags & SYNACK))
  45.             break;
  46.  
  47.         if(tcb->snd.wnd == 0){
  48.             /* Allow only one closed-window probe at a time */
  49.             if(sent != 0)
  50.                 break;
  51.             /* Force a closed-window probe */
  52.             usable = 1;
  53.         } else {
  54.             /* usable window = offered window - unacked bytes in transit
  55.              * limited by the congestion window
  56.              */
  57.             usable = min(tcb->snd.wnd,tcb->cwind) - sent;
  58.  
  59.             /* John Nagle's "single outstanding segment" rule.
  60.              * Allow only one segment in the pipeline unless there is enough
  61.              * unsent data to form at least one maximum-sized segment.
  62.              */
  63.             if(sent != 0 && tcb->sndcnt - sent < tcb->mss){
  64.                 usable = 0;
  65.             }
  66. #ifdef    notdef
  67.             /* Silly window avoidance. Don't send anything if the usable window
  68.              * is less than a quarter of the offered window.
  69.              * This test comes into play only when the offered window is at
  70.              * least 4 times the MSS; otherwise Nagle's test is sufficient
  71.              * to prevent SWS.
  72.               */
  73.             else if(usable < tcb->snd.wnd/4){
  74.                 usable = 0;
  75.             }
  76. #endif
  77.         }
  78.         /* Compute size of segment to send. This is either the usable
  79.          * window, the mss, or the amount we have on hand, whichever is less.
  80.          * (I don't like optimistic windows)
  81.          */
  82.         ssize = min(tcb->sndcnt - sent,usable);
  83.         ssize = min(ssize,tcb->mss);
  84.         dsize = ssize;
  85.  
  86.         if(ssize == 0 && !(tcb->flags & FORCE))
  87.             break;        /* No need to send anything */
  88.  
  89.         tcb->flags &= ~FORCE;    /* Only one forced segment! */
  90.  
  91.         seg.source = tcb->conn.local.port;
  92.         seg.dest = tcb->conn.remote.port;
  93.  
  94.         /* Set the SYN and ACK flags according to the state we're in. It is
  95.          * assumed that if this segment is associated with a state transition,
  96.          * then the state change will already have been made. This allows
  97.          * this routine to be called from a retransmission timeout with
  98.          * force=1.
  99.          * If SYN is being sent, adjust the dsize counter so we'll
  100.          * try to get the right amount of data off the send queue.
  101.          */
  102.         seg.flags = ACK; /* Every state except SYN_SENT */
  103.         hsize = TCPLEN;    /* Except when SYN being sent */
  104.         seg.mss = 0;
  105.  
  106.         switch(tcb->state){
  107.         case SYN_SENT:
  108.             seg.flags = 0;    /* Note fall-thru */
  109.         case SYN_RECEIVED:
  110.             if(tcb->snd.ptr == tcb->iss){
  111.                 seg.flags |= SYN;
  112.                 dsize--;
  113.                 /* Also send MSS */
  114.                 seg.mss = tcp_mss;
  115.                 hsize = TCPLEN + MSS_LENGTH;
  116.             }
  117.             break;
  118.         }
  119.         seg.seq = tcb->snd.ptr;
  120.         seg.ack = tcb->rcv.nxt;
  121.         seg.wnd = tcb->rcv.wnd;
  122.         seg.up = 0;
  123.  
  124.         /* Now try to extract some data from the send queue.
  125.          * Since SYN and FIN occupy sequence space and are reflected
  126.          * in sndcnt but don't actually sit in the send queue,
  127.          * dup_p will return one less than dsize if a FIN needs to be sent.
  128.          */
  129.         if(dsize != 0){
  130.             if(dup_p(&dbp,tcb->sndq,sent,dsize) != dsize){
  131.                 /* We ran past the end of the send queue; send a FIN */
  132.                 seg.flags |= FIN;
  133.                 dsize--;
  134.             }
  135.         } else
  136.             dbp = NULLBUF;
  137.  
  138.         /* If the entire send queue will now be in the pipe, set the
  139.          * push flag
  140.          */
  141.         if(dsize != 0 && sent + ssize == tcb->sndcnt)
  142.             seg.flags |= PSH;
  143.  
  144.         /* If this transmission includes previously transmitted data,
  145.          * snd.nxt will already be past snd.ptr. In this case,
  146.          * compute the amount of retransmitted data and keep score
  147.          */
  148.         if(tcb->snd.ptr < tcb->snd.nxt)
  149.             tcb->resent += min(tcb->snd.nxt - tcb->snd.ptr,ssize);
  150.  
  151.         tcb->snd.ptr += ssize;
  152.         /* If this is the first transmission of a range of sequence
  153.          * numbers, record it so we'll accept acknowledgments
  154.          * for it later
  155.          */
  156.         if(seq_gt(tcb->snd.ptr,tcb->snd.nxt))
  157.             tcb->snd.nxt = tcb->snd.ptr;
  158.  
  159.         /* Fill in fields of pseudo IP header */
  160.         ph.source = tcb->conn.local.address;
  161.         ph.dest = tcb->conn.remote.address;
  162.         ph.protocol = TCP_PTCL;
  163.         ph.length = hsize + dsize;
  164.  
  165.         /* Generate TCP header, compute checksum, and link in data */
  166.         if((hbp = htontcp(&seg,dbp,&ph)) == NULLBUF){
  167.             free_p(dbp);
  168.             return;
  169.         }
  170.         /* If we're sending some data or flags, start retransmission
  171.          * and round trip timers if they aren't already running.
  172.          */
  173.         if(ssize != 0){
  174.             tcb->timer.start = backoff(tcb->backoff) *
  175.              (2 * tcb->mdev + tcb->srtt + MSPTICK) / MSPTICK;
  176.             /* if(!run_timer(&tcb->timer))       always start it when value changes (PE1CHL) */
  177.                 start_timer(&tcb->timer);
  178.  
  179.             /* If round trip timer isn't running, start it */
  180.             if(!run_timer(&tcb->rtt_timer)){
  181.                 start_timer(&tcb->rtt_timer);
  182.                 tcb->rttseq = tcb->snd.ptr;
  183.             }
  184.         }
  185.         ip_send(tcb->conn.local.address,tcb->conn.remote.address,
  186.          TCP_PTCL,tcb->tos,0,hbp,ph.length,0,0);
  187.     }
  188. }
  189.